Methods for PHP to judge whether visitors are accessed by of mobile browser on mobile phone [4 methods]

  • 2021-12-04 18:13:31
  • OfStack

This paper summarizes the method of PHP to judge whether visitors are accessed by mobile phone (mobile browser). Share it for your reference, as follows:

In normal work development, we usually need to develop two different systems, PC and mobile, so as to enter different operation interfaces according to different access terminals. This requires us to first judge whether the accessed client is PC or mobile.

This blog post discusses four ways to use PHP to determine whether the accessor is a mobile (or a mobile browser) and determine whether it is a mobile access:

1. Judge according to whether HTTP_X_WAP_PROFILE exists, and if it exists, it is the mobile terminal (some service providers will shield this information)


/**
*  Is it mobile access 
* @desc  Judge whether the mobile terminal accesses or not 
* @ Method 1 : Determine if there is HTTP_X_WAP_PROFILE , there are rules 1 It must be a mobile device. 
* @return bool
* $Author: Zhihua_W
*/
function isMobile1()
{
   if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) {
     return true;
   } else {
     return false;
   }
}

2. According to the information of HTTP_VIA, if the information of VIA contains wap, it is a mobile device (some service providers will shield this information)


/**
 *  Is it mobile access 
 * @desc  Judge whether the mobile terminal accesses or not 
 * @ Method 2 : Judgment HTTP_VIA Does the information contain wap Information, there are rules 1 It must be a mobile device. 
 * @return bool
 * $Author: Zhihua_W
 */
 function isMobile2 ()
 {
   if (isset ($_SERVER['HTTP_VIA'])) {
     return true;
   } else {
     return false;
   }
 }

3. According to the client flag sent by the mobile phone, this method is stupid and the compatibility needs to be improved


/**
*  Is it mobile access 
* @desc  Judge whether the mobile terminal accesses or not 
* @ Method 3 : Determine if there is HTTP_USER_AGENT Whether the message is a client flag sent by the mobile phone, if so 1 It must be a mobile device. 
* @return bool
* $Author: Zhihua_W
*/
function isMobile3 ()
{
   if (isset ($_SERVER['HTTP_USER_AGENT'])) {
    $clientkeywords = array ('nokia', 'sony','ericsson','mot',
      'samsung','htc','sgh','lg','sharp',
      'sie-','philips','panasonic','alcatel',
      'lenovo','iphone','ipod','blackberry',
      'meizu','android','netfront','symbian',
      'ucweb','windowsce','palm','operamini',
      'operamobi','openwave','nexusone','cldc',
      'midp','wap','mobile'
      );
    //  From HTTP_USER_AGENT Find the keywords of mobile browser in 
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))){
      return true;
    } else {
      return false;
    }
  } else {
    return false;
  }
}

4. According to the protocol, this method may be incorrect or can't be judged


/**
*  Is it mobile access 
* @desc  Judge whether the mobile terminal accesses or not 
* @ Method 4 : Judgment HTTP_ACCEPT Information 
* @return bool
* $Author: Zhihua_W
*/
function isMobile4()
{
  if (isset ($_SERVER['HTTP_ACCEPT'])) {
    //  If you only support wml And does not support html That 1 It must be a mobile device 
    //  If supported wml And html But wml In html Before that, it was a mobile device 
    if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
    return true;
    } else {
    return false;
    }
  } else {
    return false;
  }
}

The above four methods all have some defects and poor compatibility, so bloggers can combine the four methods into one method to judge when using them.

PS: Here we recommend two online tools for browser judgment with similar functions for your reference:

Online browser information detection tool:
http://tools.ofstack.com/aideddesign/browser_info

Common browsers (PC, mobile) user-agent:
http://tools.ofstack.com/table/useragent

For more readers interested in PHP related contents, please check the topics of this site: "Summary of PHP Network Programming Skills", "Summary of php Regular Expression Usage", "Summary of php curl Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Tutorial on PHP Data Structure and Algorithm", "Summary of php Programming Algorithm", "Summary of php Mathematical Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: